package com.lyl.momanapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.Material;

import java.util.ArrayList;

/**
 * 名字 梁亚龙
 * 时间 2017/9/10.
 * QQ邮箱 503934353@qq.com
 */

public class TwoTwoLXAdapter extends RecyclerView.Adapter<TwoTwoLXAdapter.MyViewHolder> {
    private int num = 0;
    private ArrayList<Material.ResultBean.DataBean> list = new ArrayList<>();

    public interface OnItemClickLitener {
        void onItemClick(ImageView view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public String[] datas = null;
    private Context context;

    public TwoTwoLXAdapter(String[] datas, Context context) {
        this.datas = datas;
        this.context = context;

    }

    public void showData(ArrayList<Material.ResultBean.DataBean> datalist) {
        list.clear();
        list.addAll(datalist);
        this.notifyDataSetChanged();
    }

    public void showDataString(String[] datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_one_one_daohang_img, viewGroup, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
        if (num == position) {
            viewHolder.ll.setBackgroundColor(Color.parseColor("#F8DB42"));
        } else {
            viewHolder.ll.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num = position;
                mOnItemClickLitener.onItemClick(viewHolder.img, position);
                TwoTwoLXAdapter.this.notifyDataSetChanged();
            }
        });
        if (position == 0) {
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.drawable.lianxing1));
        } else {
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.drawable.lianxing2));
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.length;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;

        private LinearLayout ll;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.item_one_one_img);
            ll = itemView.findViewById(R.id.background_ll);
        }
    }
}
